﻿Imports System.Text
Imports System.Xml
Imports System.IO

Public Class DtsConfigClass

    Private _generatedBy As String
    Private _generatedFromPackageName As String
    Private _generatedFromPackageID As String
    Private _generatedDate As DateTime
    Private _configurations() As ConfigurationClass
    Private _configurationTable As DataTable

    Public Property GeneratedBy() As String
        Get
            Return _generatedBy
        End Get
        Set(ByVal value As String)
            _generatedBy = value
        End Set
    End Property

    Public Property GeneratedFromPackageName() As String
        Get
            Return _generatedFromPackageName
        End Get
        Set(ByVal value As String)
            _generatedFromPackageName = value
        End Set
    End Property

    Public Property GeneratedFromPackageID() As String
        Get
            Return _generatedFromPackageID
        End Get
        Set(ByVal value As String)
            _generatedFromPackageID = value
        End Set
    End Property

    Public Property GeneratedDate() As DateTime
        Get
            Return _generatedDate
        End Get
        Set(ByVal value As DateTime)
            _generatedDate = value
        End Set
    End Property

    Public Property DtsConfigurations(ByVal index As Integer) As ConfigurationClass
        Get
            Return _configurations(index)
        End Get
        Set(ByVal value As ConfigurationClass)
            _configurations(index) = value
        End Set
    End Property

    Public ReadOnly Property ConfigurationTable() As DataTable
        Get
            Return _configurationTable
        End Get
    End Property




    Public Sub New()
        '
        'Call New(string.Empty, string.Empty,string.Empty, convert.ToDateTime(String.Empty))
        ''
    End Sub

    Public Sub New(ByVal generatedBy As String, _
                   ByVal generatedFromPackageName As String, _
                   ByVal generatedFromPackageID As String, _
                   ByVal generatedDate As DateTime)
        '
        _generatedBy = generatedBy
        _generatedDate = generatedDate
        _generatedFromPackageID = generatedFromPackageID
        _generatedFromPackageName = generatedFromPackageName

    End Sub

    'Public Sub New(ByVal generatedBy As String, _
    '               ByVal generatedFromPackageName As String, _
    '               ByVal generatedFromPackageID As String, _
    '               ByVal generatedDate As DateTime, _
    '               ByVal configurations() As DtsConfigClass)
    '    '
    '    _generatedBy = generatedBy
    '    _generatedDate = generatedDate
    '    _generatedFromPackageID = generatedFromPackageID
    '    _generatedFromPackageName = generatedFromPackageName
    '    _configurations = configurations()

    'End Sub


    Public Sub AddConfiguration(ByVal configuredType As String, _
                   ByVal packagePath As String, _
                   ByVal configuredValue As String, _
                   ByVal configuredValueType As String)

        Dim c As ConfigurationClass = New ConfigurationClass(configuredType, _
                                                     packagePath, _
                                                     configuredValue, _
                                                     configuredValueType)
        If _configurations Is Nothing Then
            ReDim _configurations(0)
        Else
            ReDim Preserve _configurations(_configurations.Count)
        End If

        _configurations(_configurations.Count - 1) = c

    End Sub

    Public Sub RemoveConfiguration(ByVal index As Integer)

        If bLoading Then Exit Sub

        If _configurations Is Nothing Then
            Exit Sub
        Else
            'Dim tmp() As Object
            'ReDim tmp(UBound(_configurations) - 1)
            Dim col As Collection = New Collection
            For i As Integer = 0 To UBound(_configurations)
                If i <> index Then
                    col.Add(_configurations(i))
                End If
            Next
            ReDim _configurations(UBound(_configurations) - 1)
            For i As Integer = 1 To col.Count
                _configurations(i - 1) = col.Item(i)
            Next


        End If
    End Sub

    Public Function returnHeaderString() As String

        Dim sHeader As StringBuilder = New StringBuilder

        sHeader.Append("Generated By: " & _generatedBy & ControlChars.CrLf)
        sHeader.Append("Generated From Package Name: " & _generatedFromPackageName & ControlChars.CrLf)
        sHeader.Append("Generated From Package ID: " & _generatedFromPackageID & ControlChars.CrLf)
        sHeader.Append("Generated Date: " & _generatedDate.ToString & ControlChars.CrLf)

        Return sHeader.ToString

    End Function

    Public Sub AddConfigurationDataRow(ByVal config As ConfigurationClass)

        Dim cfg(3) As Object
        If _configurationTable Is Nothing Then
            _configurationTable = New DataTable("Configs")
            With _configurationTable
                .Columns.Add("ConfigurationType")
                .Columns.Add("Path")
                .Columns.Add("ConfigurationValueType")
                .Columns.Add("ConfiguredValue")
            End With
        End If

        cfg(0) = config.ConfiguredType
        cfg(1) = config.PackagePath
        cfg(2) = config.ConfiguredValueType
        cfg(3) = config.ConfiguredValue
        _configurationTable.Rows.Add(cfg)

    End Sub

    Public Sub WriteMe(ByVal p As String)

        Try
            Dim e As Encoding 'intentionally Null...

            If System.IO.File.Exists(p) Then
                System.IO.File.Delete(p)
            End If

            Dim xTw As New XmlTextWriter(p, e)

            xTw.Formatting = Formatting.Indented

            xTw.WriteStartDocument()
            'DTSConfiguration
            xTw.WriteStartElement("DTSConfiguration")

            'DTSConfigurationHeading
            xTw.WriteStartElement("DTSConfigurationHeading")
            'DTSConfigurationFileInfo
            xTw.WriteStartElement("DTSConfigurationFileInfo")
            xTw.WriteAttributeString("GeneratedBy", "", _generatedBy)
            xTw.WriteAttributeString("GeneratedFromPackageName", "", _generatedFromPackageName)
            xTw.WriteAttributeString("GeneratedFromPackageID", "", _generatedFromPackageID)
            xTw.WriteAttributeString("GeneratedDate", "", _generatedDate)
            xTw.WriteEndElement() 'DTSConfigurationFileInfo
            xTw.WriteEndElement() 'DTSConfigurationHeading


            For Each config As ConfigurationClass In _configurations
                xTw.WriteStartElement("Configuration")
                With config
                    xTw.WriteAttributeString("ConfiguredType", "", .ConfiguredType)
                    xTw.WriteAttributeString("Path", "", .PackagePath)
                    xTw.WriteAttributeString("ValueType", "", .ConfiguredValueType)
                    xTw.WriteStartElement("ConfiguredValue")
                    xTw.WriteString(.ConfiguredValue)
                    xTw.WriteEndElement() 'ConfiguredValue
                End With
                xTw.WriteEndElement() 'Configuration
            Next

            xTw.WriteEndElement() 'DTSConfiguration
            xTw.WriteEndDocument() 'End Document
            xTw.Flush() 'Write to file
            xTw.Close()


        Catch Ex As Exception
            Throw
        End Try

    End Sub

    Public Sub FormatSSISConfigFile(ByVal p As String)

        Dim xD As Xml.XmlDocument = New Xml.XmlDocument
        xD.Load(p)

        If System.IO.File.Exists(p) Then
            System.IO.File.Delete(p)
        End If

        Dim xTw As XmlTextWriter = New XmlTextWriter(p, Nothing)
        xTw.Formatting = Formatting.Indented
        xD.Save(xTw)

    End Sub

End Class
